
#include <iostream>
#include <list>
#include <vector>
#include <string>

using namespace std;

enum INFO_TYPE{
	NONE,
	RESOURCE,
	HELP
};

class AllyCenter;

// 抽象观察者 Observer
class Observer
{
public:
	Observer(){}
	// 声明抽象方法
	virtual void call(INFO_TYPE infoType, AllyCenter* ac) = 0;
	string getName(){
		return name;
	}
	void setName(string iName){
		this->name = iName;
	}
private:
	string name;
};

// 抽象目标：联盟中心
class AllyCenter
{
public:
	AllyCenter();
	// 声明通知方法
	virtual void notify(INFO_TYPE infoType, std::string name) = 0;
	// 加入玩家
	void join(Observer* player);
	// 移除玩家
	void remove(Observer* player);
protected:
	// 玩家列表
	std::vector<Observer*>playerList;
};


// 具体观察者 
class Player :public Observer
{
public:
	Player(){
		setName("none");
	}
	Player(string iName){
		setName(iName);
	}
	// 实现
	void call(INFO_TYPE infoType, AllyCenter* ac){
		switch (infoType){
		case RESOURCE:
			printf("%s :我这里有物资\n", getName().c_str());
			break;
		case HELP:
			printf("%s :救救我\n", getName().c_str());
			break;
		default:
			printf("Nothing\n");
		}
		ac->notify(infoType, getName());
	}
	// 实现具体方法
	void help(){
		printf("%s:坚持住，我来救你！\n", getName().c_str());
	}
	void come(){
		printf("%s:好的，我来取物资\n", getName().c_str());
	}
};


AllyCenter::AllyCenter(){
	printf("大吉大利，今晚吃鸡!\n");
}

// 加入玩家
void AllyCenter::join(Observer* player){
	if (playerList.size() == 4){
		printf("玩家已满\n");
		return;
	}
	printf("玩家 %s 加入\n", player->getName().c_str());
	playerList.push_back(player);
	if (playerList.size() == 4){
		printf("组队成功，不要怂，一起上！\n");
	}
}
// 移除玩家
void AllyCenter::remove(Observer* player){
	printf("玩家 %s 退出\n", player->getName().c_str());
	//playerList.remove(player);
}

// 具体目标
class AllyCenterController :public AllyCenter
{
public:
	AllyCenterController();
	// 实现通知方法
	void notify(INFO_TYPE infoType, std::string name);
};



AllyCenterController::AllyCenterController(){}

// 实现通知方法
void AllyCenterController::notify(INFO_TYPE infoType, std::string name){
	switch (infoType){
	case RESOURCE:
		for (auto obs : playerList){
			if (obs->getName() != name){
				((Player*)obs)->come();
			}
		}
		break;
	case HELP:
		for (auto obs : playerList){
			if (obs->getName() != name){
				((Player*)obs)->help();
			}
		}
		break;
	default:
		printf("Nothing\n");
	}
}

int main()
{
	// 创建一个战队
	AllyCenterController* controller = new AllyCenterController();

	// 创建4个玩家，并加入战队
	Player* Jungle = new Player("Jungle");
	Player* Single = new Player("Single");
	Player* Jianmengtu = new Player("贱萌兔");
	Player* SillyDog = new Player("傻子狗");
	controller->join(Jungle);
	controller->join(Single);
	controller->join(Jianmengtu);
	controller->join(SillyDog);

	printf("\n\n");

	// Jungle发现物资，呼叫队友
	Jungle->call(RESOURCE, controller);

	printf("\n\n");

	// 傻子狗遇到危险，求救队友
	SillyDog->call(HELP, controller);

	printf("\n\n");
	system("pause");
	return 0;
}








